package com.irssproject.app.web.action.lectorRSS;

import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.irssproject.app.entitats.Item;
import com.irssproject.app.entitats.Reading;
import com.irssproject.app.entitats.Subscription;
import com.irssproject.app.service.lectorRSS.iface.SubscriptionsService;
import com.irssproject.app.service.recommendations.iface.RecommendationsService;
import com.irssproject.app.utils.Constants;
import com.irssproject.app.web.action.JSONWrappers.FeedJSON;
import com.irssproject.app.web.action.JSONWrappers.ItemJSON;
import com.irssproject.app.web.action.JSONWrappers.ReadingJSON;
import com.irssproject.app.web.action.JSONWrappers.SubscriptionJSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ShowReadingAction extends ActionSupport {

    private Long idLectura;
    private Long idSubscripcio;
    private Long idItem;
    private String cont;
    boolean llegit;
    private ItemJSON item = new ItemJSON();
    private ReadingJSON lectura;
    private String listItems;
    Map<String, Object> session;
    private String etiquetaPersonalitzada;
    private String fraseLectura;
    SubscriptionsService subscripcioService;
    RecommendationsService recommendationsService;
    Logger logger = Logger.getLogger(ShowReadingAction.class.getName());

    public ShowReadingAction(SubscriptionsService subscripcioService, RecommendationsService recommendationsService) {
        this.subscripcioService = subscripcioService;
        this.recommendationsService = recommendationsService;
    }

    public boolean isLlegit() {
        return llegit;
    }

    public void setLlegit(boolean llegit) {
        this.llegit = llegit;
    }

    public Long getIdLectura() {
        return idLectura;
    }

    public void setIdLectura(Long idLectura) {
        this.idLectura = idLectura;
    }

    public ItemJSON getItem() {
        return item;
    }

    public void setItem(ItemJSON item) {
        this.item = item;
    }

    public String getCont() {
        return cont;
    }

    public void setCont(String cont) {
        this.cont = cont;
    }

    public void setListItems(String listItems) {
        this.listItems = listItems;
    }

    public String getListItems() {
        return listItems;
    }

    public void setLectura(ReadingJSON lectura) {
        this.lectura = lectura;
    }

    public ReadingJSON getLectura() {
        return lectura;
    }

    public void setEtiquetaPersonalitzada(String etiquetaPersonalitzada) {
        this.etiquetaPersonalitzada = etiquetaPersonalitzada;
    }

    public String getEtiquetaPersonalitzada() {
        return etiquetaPersonalitzada;
    }

    public void setFraseLectura(String fraseLectura) {
        this.fraseLectura = fraseLectura;
    }

    public String getFraseLectura() {
        return fraseLectura;
    }

    public Long getIdSubscripcio() {
        return idSubscripcio;
    }

    public void setIdSubscripcio(String idSubscripcio) {
        this.idSubscripcio = new Long(idSubscripcio);
    }

    public Long getIdItem() {
        return idItem;
    }

    public void setIdItem(Long idItem) {
        this.idItem = idItem;
    }

    public String mostrarLectura() {

        try {
            session = ActionContext.getContext().getSession();

            subscripcioService.markItemAsRead(idLectura);

            Reading lecturaAux = subscripcioService.getReadingById(idLectura);
            //Posem a no la variable de sessio que guarda el vot
            session.put(Constants.MAGRADA, lecturaAux.isLiked());

            ItemJSON itemlocal = new ItemJSON();

            SubscriptionJSON subscripcio = new SubscriptionJSON();

            lectura = new ReadingJSON();

            itemlocal.setTitle(lecturaAux.getItem().getTitle());
            itemlocal.setLink(lecturaAux.getItem().getLink());
            itemlocal.setDataPublicacioDisplay(lecturaAux.getItem().getDataPublicacioString());
            itemlocal.setContent(lecturaAux.getItem().getContent());
            lectura.setItem(itemlocal);
            lectura.setLlegida(lecturaAux.isRead());
            lectura.setValorada(lecturaAux.isLiked());


            subscripcio.setNumItemsNoLlegits(lecturaAux.getSubscription().getUnreadItemsNumber());
            subscripcio.setFeed(new FeedJSON(lecturaAux.getSubscription().getFeed()));
            subscripcio.setId(lecturaAux.getSubscription().getId());
            subscripcio.setEtiqueta(lecturaAux.getSubscription().getLabel());
            lectura.setSubscripcio(subscripcio);
            lectura.setId(lecturaAux.getId());

        } catch (Exception e) {
            logger.severe("Error al mostrar lectura");
        }

        return SUCCESS;
    }

    public String marcarLlegidaONoLlegida() {

        session = ActionContext.getContext().getSession();

        boolean isLlegida = subscripcioService.setReadingReadOrUnread(idLectura);

        setLlegit(isLlegida);

        return SUCCESS;
    }

    public String markAsReaded(){
        subscripcioService.markItemAsRead(idLectura);
        return SUCCESS;
    }
    
    public String etiquetarFraseLectura() {

        try {
            logger.log(Level.INFO, "Afegim l''etiqueta {0} a la frase {1}", new Object[]{getEtiquetaPersonalitzada(), getFraseLectura()});
            Subscription subscripcio = subscripcioService.getSubscriptionById(getIdSubscripcio());
            recommendationsService.insertPersonalizedLabel(subscripcio, getEtiquetaPersonalitzada(), getFraseLectura());

        } catch (Exception ex) {
            logger.severe("Error al etiquetar una frase de la lectura");
            ex.printStackTrace();
        }

        return SUCCESS;
    }

    public String mostrarItem() {

        Item aux = new Item();
        aux = subscripcioService.getItemById(idItem);
        item.setId(aux.getId());
        item.setTitle(aux.getTitle());
        item.setLink(aux.getLink());
        item.setDataPublicacioDisplay(aux.getDataPublicacioString());
        item.setContent(aux.getContent());

        return SUCCESS;
    }
}
